var express = require('express')
var send_sms = require('./../tool/mobileVerificationCode')
var User = require('./../models/User')
var md5 = require('blueimp-md5')

var router = express.Router()
//发送短信手机验证码
router.get('/sendVerificationCode',(req,res) => {
    //获取手机号
    var telephone = req.query.telephone
    if(!telephone) return res.send('error')
    req.session.verCode = null
    req.session.verCode = {
        value : 123456,
        telephone : telephone
    }
    return res.json({status : 0,code : 0})
    //send_sms(telephone,function(data,verCode){
    //    req.session.verCode = {
    //        value : verCode,
    //        telephone : telephone
    //    }
    //    var result = JSON.parse(data)
    //    result.status = 0
    //    return res.json(result)
    //},function(){
    //    return res.json({status : 1})
    //})
})
//验证手机验证码
router.get('/checkVerCode', (req,res) => {
    var verCode = req.query.verCode
    var telephone = req.query.telephone
    if(!verCode || !telephone) return res.json({status : -1})
    var correctVerCode = req.session.verCode && req.session.verCode.value
    var correctTelephone = req.session.verCode && req.session.verCode.telephone
    if(!correctVerCode || !correctTelephone) return res.json({status : -1})
    //如果手机号与此验证码绑定的手机号不符,那么直接打回,让用户重新发送验证码或更改手机号
    if(correctTelephone + '' !== telephone) return res.json({status : -1})
    if(verCode !== correctVerCode + '') return res.json({status : 1})
    return res.json({status : 0})
})
//校验昵称
router.get('/checkNickname',(req,res) => {
    var value = req.query.value
    if(!value) return res.send('error')
    User.findOne({
        nickname : value
    },function(err,docs){
        if(err) return res.json({status : -1})
        if(docs !== null) return res.json({status : 1})
        return res.json({status : 0})
    })
})
//校验手机号
router.get('/checkTelephone',(req,res) => {
    var value = req.query.value
    if(!value) return res.send('error')
    User.findOne({
        telephone : value
    },function(err,docs){
        if(err) return res.json({status : -1})
        if(docs !== null) return res.json({status : 1})
        return res.json({status : 0})
    })
})
//验证表单数据并插入数据库
router.post('/add', (req,res) => {
    //获取表单数据
    var body = req.body
    var nickname = body['params[nickname]']
    var telephone = body['params[telephone]']
    var password = body['params[password]']
    if(!nickname || !telephone || !password)
        //请完善表单信息
        return res.json({status : -1})
    if(!req.session.verCode || !req.session.verCode.telephone) return res.json({status : -1})
    //校验手机号和session中的手机号是否相同
    if(req.session.verCode.telephone + '' !== telephone) return res.json({status : 1})
    var user = new User({nickname : nickname,telephone : telephone, password : md5(md5(password))})
    user.save(function(err,docs){
        if(err) return res.json({status : -1})
        req.session.verCode = null
        return res.json({status : 0})
    })
})





module.exports = router
